package com.base.balibrary.view.custom.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.base.balibrary.R;

/**
 * BAndroid
 * Created by YichenZ on 2015/7/24 10:55.
 */
public class FreedomLocationDialog extends Dialog {
    //相对位置
    private View view;
    private int layoutResId;
    WindowManager.LayoutParams params;
    Window dialogWindow;
    //1 top,0 center,2 bottom
    short showType = 0;
    View contentView;

    public FreedomLocationDialog(Context context) {
        this(context, R.style.VIPDialog);
    }

    public FreedomLocationDialog(Context context, int theme) {
        super(context, theme);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void onStart() {
        super.onStart();
        setAttr();
    }

    public void initUI(int layoutResId) {
        this.layoutResId = layoutResId;
        if (layoutResId < -1) {
            new RuntimeException("Please set initUI()");
        }
        LayoutInflater mInflater = (LayoutInflater) this.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        contentView = mInflater.inflate(layoutResId, null);
        setContentView(contentView);

    }

    private void setAttr() {
        if (dialogWindow == null) {
            dialogWindow = this.getWindow();
        }
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        if (params == null) {
            params = new WindowManager.LayoutParams();
        }
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        int[] location = new int[2];
        if(view!=null) {
            view.getLocationInWindow(location);
        }
        switch (showType) {
            case 0:
                break;
            case 1: {
                params.x = 0;
                params.y = (int) location[1] + (view.getHeight() / 2);
                params.gravity = Gravity.TOP;
                dialogWindow.setAttributes(params);
                break;
            }
            case 2: {
                params.x = 0;
                params.y = -((int) location[1] + (view.getHeight() / 2));
                params.gravity = Gravity.BOTTOM;
                dialogWindow.setAttributes(params);
                break;
            }
        }
    }

    public void showDialogOfTheViewBottom(View view) {
        showType = 1;
        this.view = view;
        super.show();
    }

    public void show() {
        showType = 0;
        super.show();
    }

    public void showDialogOfTheViewTop(View view) {
        showType = 2;
        this.view = view;
        super.show();
    }

    public View getContentView(){
        if(contentView==null)
            new RuntimeException("contentView is null");
        return contentView;
    }

}
